package com.ifreshwing.view.CheckedTextView;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.ifreshwing.bean.ReasonBean;
import com.ifreshwing.userpark.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lz on 2017/5/2.
 */

public class CheckedTextView extends LinearLayout {
    List<ReasonBean.ResultBean> list;

    public CheckedTextView(Context context) {
        super(context);
    }

    public CheckedTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void showView(List<ReasonBean.ResultBean> list) {
        this.list = list;
        setBackground(getResources().getDrawable(R.color.colorWhite));
        setGravity(Gravity.CENTER_VERTICAL);
        setPadding(32, 32, 32, 32);
        //最外层ll
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER);
        for (int i = 0; i < list.size(); i++) {
            // item
            // item ll
            LinearLayout layout = new LinearLayout(getContext());
            layout.setOrientation(LinearLayout.HORIZONTAL);
            layout.setGravity(Gravity.CENTER_VERTICAL);
            // item cb
            final CheckBox checkBox = new CheckBox(getContext());
            checkBox.setScaleX((float) 0.75);
            checkBox.setScaleY((float) 0.75);
            //item ll tv
            final SelectTextView textView = new SelectTextView(getContext());
            textView.setText(list.get(i).getTag());
            textView.setTag(list.get(i).getId());
            textView.setOnClickListener(v -> {
                if (textView.i == 1) {
                    textView.setTextColor(Color.GRAY);
                    textView.i = 0;
                    checkBox.setChecked(false);
                } else if (textView.i == 0) {
                    textView.setTextColor(Color.RED);
                    textView.i = 1;
                    checkBox.setChecked(true);
                }
            });
            checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (isChecked) {
                    textView.setTextColor(Color.RED);
                    textView.i = 1;

                } else {
                    textView.setTextColor(Color.GRAY);
                    textView.i = 0;
                }
            });

            layout.addView(checkBox);
            layout.addView(textView);
            linearLayout.addView(layout);
        }
        this.addView(linearLayout);
    }

    public String getCheckItems() {
        SelectTextView cSelectTextView;
        List<String> rList = new ArrayList<>();
        String ids = "";
        LinearLayout linearLayout = (LinearLayout) getChildAt(0);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            LinearLayout child_ll = (LinearLayout) linearLayout.getChildAt(i);
            cSelectTextView = (SelectTextView) child_ll.getChildAt(1);
            if (cSelectTextView.GetCheckStatus() == 1) {
                rList.add(cSelectTextView.getTag().toString());
            }
        }
        for (int i = 0; i < rList.size(); i++) {
            ids += rList.get(i) + ",";
        }
        return ids;
    }

    public void setChecked(List<String> s) {
        SelectTextView cSelectTextView;
        CheckBox checkBox;
        for (String attrId : s) {
            LinearLayout linearLayout = (LinearLayout) getChildAt(0);
            for (int i = 0; i < linearLayout.getChildCount(); i++) {
                LinearLayout child_ll = (LinearLayout) linearLayout.getChildAt(i);
                checkBox = (CheckBox) child_ll.getChildAt(0);
                cSelectTextView = (SelectTextView) child_ll.getChildAt(1);
                if (attrId.equals(cSelectTextView.getTag().toString())) {
                    checkBox.setChecked(true);
                    cSelectTextView.setTextColor(Color.RED);
                }
            }
        }
    }

}
